const app = getApp()
import BaseModel from '../../utils/common.js'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    swiperIndex:"",//banner轮播
    bannerImgs: [], //banner数组
    indicatorDots: true,
    autoplay: true,
    interval: 3000,
    duration: 500,
    selectText: "搜索课程",
    selectIcon: "search",
    navData:[],//一级导航数据
    navDataId:1, //默认是全国数据
    navTabList:[],//二级导航数据
    page:1,
    size:6, //每页条数
    is_country:1,//全国传1,分社传2
    type_twoIndex:0,//二级导航选中索引
    type_two:0,//二级导航选中id
    linedownAllData:[], //活动数组
    is_clickTab:0,//判断是否切换tab  0是切换，重新拿数据  1是上拉，追加数据
    platform:"", //机型
    windowHeight:"",
    windowWidth:"",
    navScrollLeft: 0,
    tabFixed:false, //固定导航
    navHeight:"",//高度
    footer_more:"",
    empty_title:"",
    empty_img:"",
    empty_again:"",//重试
    companyRole:"",//公司角色
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      platform:wx.getStorageSync('platform') //判断机型
    })
    wx.getSystemInfo({
      success: (res) => {
        this.setData({
          windowHeight: res.windowHeight,
          windowWidth: res.windowWidth
        })
      },
    })
  },

  onShow: function () {
    this.getCompanyData(); //分公司角色获取
    this.getNavTabData();//获取二级导航
    this.getBannerData(); //banner
    this.getlinedownAllData(); //线下活动
  },
  //获取分公司角色  主办方类型1：总公司2：子公司3：分公司
  getCompanyDataModel:new BaseModel({
    url:"employees/organizer_type",
    method:"get",
    needAjaxLoading:true,
    loadingText:"玩命加载中"
  }),
  getCompanyData(){
    this.getCompanyDataModel
        .fetch({  })
        .then(res => {
          //成功 (1分总2普通员工 3财务)
          app.globalData.companyRole=res.data.type;
          this.setData({
            companyRole:res.data.type
          })
          console.log(app.globalData.companyRole)
        })
        .catch(res => {
        })
  },
  /*点击切换一级tab*/
  nav_tabSwitch:function(e){
    this.setData({
      navDataId:e.currentTarget.dataset.id,
      is_country:e.currentTarget.dataset.id,
      type_two:0,
      type_twoIndex:0,
      is_clickTab:0,
      page:1,
    })
    this.getNavTabData();
    this.getlinedownAllData();
  },
  /*获取导航二级菜单*/
  getNavTabDataModel: new BaseModel({
    url: 'Business/getBusiness',
    method: "get",
    needAjaxLoading:true,
    loadingText:"玩命加载中"
  }),
  getNavTabData() {
    this.getNavTabDataModel
        .fetch({
          pid:this.data.navDataId
        })
        .then(res => {
          var arr = [{
            icon:"layui-icon-comp",
            id:0,
            name:"全部",
            pid:1,
            type:2
          }];
          for(var i = 0; i < res.data.length;i++){
            arr.push(res.data[i])
          }
          //成功
          this.setData({
            navTabList:arr
          })
        })
        .catch(res => {
          //失败
        })
  },
  /*banner轮播样式*/
  swiperChange(e) {
    const that = this;
    that.setData({
      swiperIndex: e.detail.current,
    })
  },
  //活动banner
  getBannerModel: new BaseModel({
    url: 'getBanner',
    method: "get",
    needAjaxLoading:true,
    loadingText:"玩命加载中"
  }),
  getBannerData() {
    this.getBannerModel
        .fetch({ "type": "E" })
        .then(res => {
          //成功
          this.setData({
            bannerImgs: res.data
          })
        })
        .catch(res => {
          //失败
        })
  },

  /*点击切换二级导航 不带滑动*/
  nav_tabNav:function(e){
    this.setData({
      type_two:e.currentTarget.dataset.id,
      is_clickTab:0,
      page:1,
    })
    this.getlinedownAllData();
  },
  /*导航固定在头部*/
  /*onPageScroll:function(e){
    var scrollTop= e.scrollTop;
    console.log(scrollTop)
    var that = this;
    //创建节点选择器
    var query = wx.createSelectorQuery();
    //选择id
    query.select('#myText').boundingClientRect()
    query.exec(function (res) {
      //取高度
      that.setData({
        navHeight:res[0].height
      })
    })
    if(scrollTop>=that.data.navHeight&&!that.data.tabFixed){
      that.setData({
        tabFixed:true
      })
    }
    if(scrollTop<that.data.navHeight&&that.data.tabFixed){
      that.setData({
        tabFixed:false
      })
    }
  },*/

  /*线下活动全国*/
  getlinedownAllModel: new BaseModel({
    url: 'employees/listActivity',
    method: "get",
    needAjaxLoading:true,
    loadingText:"玩命加载中"
  }),
  getlinedownAllData() {
    this.getlinedownAllModel
        .fetch({
          "type_two":this.data.type_two,
          "is_country":this.data.is_country,
          "page": this.data.page,
          "size": this.data.size
        })
        .then(res => {
          if(this.data.is_clickTab==0){
            this.setData({
              linedownAllData: res.data,
              empty_title:"暂无数据",
              empty_img:"http://hwj2018.oss-cn-beijing.aliyuncs.com/hwj2019/icon_new3.0/empty_mes.png",
              empty_again:"",//重试
            })
          }else{
            if(res.data==""||res.data==null){
              this.setData({
                footer_more:"没有更多数据了"
              })
            }else{
              var linedownAllData_list = this.data.linedownAllData;
              for(var i=0;i<res.data.length;i++){
                linedownAllData_list.push(res.data[i])
              }
              this.setData({
                linedownAllData: linedownAllData_list
              })
            }
          }
        })
        .catch(res => {
          //失败
          this.setData({
            empty_title:"请求失败",
            empty_img:"http://hwj2018.oss-cn-beijing.aliyuncs.com/hwj2019/icon/empty_data.png",
            empty_again:"重试",//重试
          })
        })
  },
//重试
  again_request:function(){
    this.setData({
      is_clickTab:0,
      page:1
    })
    this.getNavTabData();//获取二级导航
    this.getBannerData(); //banner
    this.getlinedownAllData(); //线下活动
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      is_clickTab:0,
      page:1
    })
    this.getNavTabData();//获取二级导航
    this.getBannerData(); //banner
    this.getlinedownAllData(); //线下活动
    wx.stopPullDownRefresh(); //停止下拉
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    that.data.page+=1;
    this.setData({
      is_clickTab:1  //上拉加载标识
    })
    this.getlinedownAllData();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },

  /**
   * 生命周期函数--监听页面显示
   */

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

})